package com.android.view.activity.trade;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.model.entity.res.AccountInfoResult;
import com.android.model.entity.res.AccountTokenResult;
import com.android.model.entity.res.CreateTokenResult;
import com.android.model.info.LocalSp;
import com.android.presenter.info.AccountInfoPresenter;
import com.android.presenter.info.AccountTokenPresenter;
import com.android.presenter.info.CreateTokenPresenter;
import com.android.presenter.iview.IAccountInfoView;
import com.android.presenter.iview.IAccountTokenView;
import com.android.presenter.iview.ICreateTokenView;
import com.android.view.activity.base.ToolbarBaseActivity;
import com.android.view.activity.user.LoginActivity;
import com.android.view.fragment.trade.TradeBuyFragment;
import com.android.view.fragment.trade.TradeSellFragment;
import com.android.view.widget.CommonHeaderView;
import com.android.R;
import com.android.view.fragment.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 交易页面
 * Created by TenFace on 2018/4/27.
 */

public class TradeActivity extends ToolbarBaseActivity implements IAccountTokenView, ICreateTokenView, IAccountInfoView {

    @BindView(R.id.tabLayout)
    TabLayout mTab;
    @BindView(R.id.custom_header_tv_right_btn)
    TextView custom_header_tv_right_btn;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.tradeCommonHeader)
    CommonHeaderView tradeCommonHeader;
    private AccountTokenPresenter accountTokenPresenter;
    private CreateTokenPresenter createTokenPresenter;
    private AccountInfoPresenter accountInfoPresenter;
    List<Fragment> listFragment;
    TradePagerAdapter fragmentAdapter;
    private String accountToken;
    private static String intentKey = "SYMBOL";
    private static String intentKey2 = "TYPE";
    private String type;
    private String title;
    private Context context;

    public static void actionStart(Context context, String symbol, String type) {
        Intent intent = new Intent(context, TradeActivity.class);
        intent.putExtra(intentKey, symbol);
        intent.putExtra(intentKey2, type);
        context.startActivity(intent);
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_trade);
    }

    @Override
    protected void initView() {
        context = this;
        Intent intent = getIntent();
        title = intent.getStringExtra(intentKey);
        type = intent.getStringExtra(intentKey2);
        tradeCommonHeader.setTitle(title + " / " + type + " ▾");
        tradeCommonHeader.setRightText("Order History");
        initViewPager();
    }

    @Override
    protected void obtainData() {
        mTab.setupWithViewPager(mViewPager);
        accountTokenPresenter = new AccountTokenPresenter(this);
        createTokenPresenter = new CreateTokenPresenter(this);
        accountInfoPresenter = new AccountInfoPresenter(this);
    }

    @Override
    protected void initEvent() {
        codeTimer.start();
        custom_header_tv_right_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ssoToken = LocalSp.getInstance(context).getSSOToken();
                if (!TextUtils.isEmpty(ssoToken)) {
                    startActivity(new Intent(context, OrderHistoryActivity.class));
                } else {
                    startActivity(new Intent(context, LoginActivity.class));
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (codeTimer != null) {
            codeTimer.cancel();
        }
    }

    private void initViewPager() {
        listFragment = new ArrayList<>();
        listFragment.add(TradeBuyFragment.newInstance(title, type));
        listFragment.add(TradeSellFragment.newInstance(title, type));

        fragmentAdapter = new TradePagerAdapter(getSupportFragmentManager(), listFragment);
        mViewPager.setAdapter(fragmentAdapter);
//        mViewPager.addOnPageChangeListener(new MyPageChangeListener());
        mViewPager.setOffscreenPageLimit(2);
    }

    @Override
    public void createAccountTokenSuccess(AccountTokenResult accountTokenResult) {
        if (accountTokenResult != null) {
            accountToken = accountTokenResult.getValue();
            if (!TextUtils.isEmpty(accountToken)) {
                accountInfoPresenter.queryAccountInfo(getContext(), accountToken);
            }
        }
    }

    @Override
    public void createAccountTokenFail(int status, String desc) {

    }

    @Override
    public void getAccountInfoSuccess(AccountInfoResult accountInfoResult) {
        String accountId = accountInfoResult.getAccountId();
        if (!TextUtils.isEmpty(accountId) && !TextUtils.isEmpty(accountToken)) {
            createTokenPresenter.createToken(getContext(), accountId, "ORDER", accountToken);
        }
    }

    @Override
    public void getAccountInfoFail(int status, String desc) {

    }

    @Override
    public void createTokenSuccess(CreateTokenResult createTokenResult) {

    }

    @Override
    public void createTokenFail(int status, String desc) {

    }


    private class TradePagerAdapter extends FragmentStatePagerAdapter {
        public String[] mTilte;
        private List<Fragment> fragments;

        public TradePagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            fragments = fragmentList;
            mTilte = getResources().getStringArray(R.array.tab_trade_Title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTilte[position];
        }

        @Override
        public BaseFragment getItem(int position) {
            return (BaseFragment) fragments.get(position);
        }

        @Override
        public int getCount() {
            return 2;
        }
    }

    private CountDownTimer codeTimer = new CountDownTimer(500 * 60 * 60 * 1000, 20 * 60 * 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            String ssoToken = LocalSp.getInstance(context).getSSOToken();
            if (!TextUtils.isEmpty(ssoToken)) {
                accountTokenPresenter.createAccountToken(context, ssoToken);
            }
        }

        @Override
        public void onFinish() {

        }
    };

}
